Imports Order2GoWrapper.FXCMAccount

Public Class Form1
    Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        GlobalVariablesEndingProcedures()
    End Sub
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        StartGlobalVariables()
        Me.ExchangeRateViewer1.CurrencyPairs = General.CurrencyPairs
        Me.ExchangeRateViewer1.LoadCurrencyPairs()
        AddHandler General.YahooFinanceExchangeRatesReaded, AddressOf Sub_YahooFinanceQuotesReaded

        Me.BasicCurrencyConversor1.Currencies = General.Currencies
        Me.BasicCurrencyConversor1.CurrencyPairs = General.CurrencyPairs
        Me.BasicCurrencyConversor1.LoadCurrencies()


        Me.Chart1.Setup()
        Me.Chart1.CurrencyPair = General.CurrencyPairs(CurrencyPair.ECurrencyPairSymbol.EURUSD)
        Me.Chart1.Timeframe = Chart.ETimeframe.M5
        Dim d As Date
        d = Now
        Me.Chart1.ChangeChartLimits(d.AddMinutes(90), d.AddMinutes(-90), 0.87, 0.86)
        Me.Chart1.Candles = New SortedDictionary(Of Date, Candle)
        Me.Chart1.Candles.Add(d, New Candle(0.8675, 0.868, 0.8677, 0.8674))
        Me.Chart1.Candles.Add(d.AddMinutes(-5), New Candle(0.8677, 0.8685, 0.868, 0.867))
        Me.Chart1.Candles.Add(d.AddMinutes(-10), New Candle(0.868, 0.8692, 0.867, 0.8665))

    End Sub
    Private Sub Sub_YahooFinanceQuotesReaded()
        Me.ExchangeRateViewer1.LoadSelectedCurrencyPairExchangeRate()
    End Sub


#Region "Order2Go testings"
    Dim FXCMAcc As Order2GoWrapper.FXCMAccount
    Private Sub BtnLoginFXCM_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnLoginFXCM.Click
        Me.LblFXCMStatus.Text = "Logging In..."
        FXCMAcc = New Order2GoWrapper.FXCMAccount(Me.TxtFXCMUsername.Text, Me.TxtFXCMPassword.Text, CType([Enum].Parse(GetType(EAccountType), Me.CmbFXCMAccountType.Text.ToString), EAccountType))
        If FXCMAcc.OpenTradingDesk Then
            Me.LblFXCMStatus.Text = "Logged In"
        Else
            Me.LblFXCMStatus.Text = FXCMAcc.LastErrorMessage
        End If
    End Sub
    Private Sub BtnLogOutFXCM_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnLogOutFXCM.Click
        Me.LblFXCMStatus.Text = "Logging Out..."
        If FXCMAcc.CloseTradingDesk Then
            Me.LblFXCMStatus.Text = "Logged Out"
        Else
            Me.LblFXCMStatus.Text = FXCMAcc.LastErrorMessage
        End If
    End Sub
#End Region
#Region "Metatrader 4 DDE"
    Dim WithEvents MT4ExchangeRatesProvider As Metatrader4DDEWrapper.MetatraderExchangeRatesProvider
    Private Sub BtnStartMetatraderExchangeRatesListening_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnStartMetatraderExchangeRatesListening.Click
        MT4ExchangeRatesProvider = New Metatrader4DDEWrapper.MetatraderExchangeRatesProvider
        Me.LblMetatraderExchangeRatesListeningInfo.Text = MT4ExchangeRatesProvider.ProviderStatus.ToString
    End Sub
    Private Sub MT4ExchangeRatesProvider_Disconnected(ByVal sender As Object) Handles MT4ExchangeRatesProvider.Disconnected
        Me.LblMetatraderExchangeRatesListeningInfo.Text = MT4ExchangeRatesProvider.ProviderStatus.ToString & ": " & Me.MT4ExchangeRatesProvider.LastErrorMessage
    End Sub
    Private Sub BtnCheckConnectionMetatraderExchangeRatesListening_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCheckConnectionMetatraderExchangeRatesListening.Click
        Me.LblMetatraderExchangeRatesListeningInfo.Text = MT4ExchangeRatesProvider.ProviderStatus.ToString & ": " & Me.MT4ExchangeRatesProvider.LastErrorMessage
    End Sub
    Delegate Sub DAppendTextDelegate(ByVal TextToAppend As String)
    Private Sub MT4ExchangeRatesProvider_NewExchangeRateDataArrived(ByVal CurrencyPairString As String, ByVal ServerDate As Date, ByVal BidPrice As Double, ByVal AskPrice As Double) Handles MT4ExchangeRatesProvider.NewExchangeRateDataArrived
        CurrencyPairs(CurrencyPair.GetCurrencyPairSymbolFromCurrencyPairString(CurrencyPairString)).UpdateFromRealTimeProvider(BidPrice, AskPrice, ServerDate, CurrencyPair.EExchangeRateProvider.Metatrader4)
        'uso el mSelectedCurrencyPair para evitar lios de threas, aparentemente este procedimiento se ejecuta en una thread diferente
        SyncLock Me.TxtMetatraderExchangeRatesListeningHistory
            Me.TxtMetatraderExchangeRatesListeningHistory.Invoke(CType(AddressOf Me.TxtMetatraderExchangeRatesListeningHistory.AppendText, DAppendTextDelegate), CurrencyPairString & " " & ServerDate.ToLongTimeString & " " & Now.ToLongTimeString & " " & BidPrice.ToString("##0.0000") & " " & AskPrice.ToString("##0.0000") & vbCrLf)
        End SyncLock
        If Me.ExchangeRateViewer1.mSelectedCurrencyPair.ToUpper = CurrencyPairString.ToUpper Then
            SyncLock Me.ExchangeRateViewer1 ' no se si esto sea necesario, lo dejo por si las dudas
                Me.ExchangeRateViewer1.Invoke(CType(AddressOf Me.ExchangeRateViewer1.LoadSelectedCurrencyPairExchangeRate, MethodInvoker))
            End SyncLock
        End If
    End Sub
    Private Sub BtnStopMetatraderExchangeRatesListening_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnStopMetatraderExchangeRatesListening.Click
        MT4ExchangeRatesProvider.StopExchangeRatesListening()
        Me.LblMetatraderExchangeRatesListeningInfo.Text = MT4ExchangeRatesProvider.ProviderStatus.ToString
    End Sub
#End Region


 

End Class
